<?php
/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Framework
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Currency_Exception
 */
importModule('Currency.Exception');

/**
 * Hoa_Stream_Context
 */
import('Stream.Context');

/**
 * Hoa_File_Read
 */
import('File.Read.~');

/**
 * Hoa_Xml
 */
import('Xml.~');

/**
 * Class Hoathis_Currency
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Currency
 */
class Hoathis_Currency {

    /*
       * Constant URL
       * String
    */
    const URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';

    /*
       * Variable $data
       * Array
    */
    private $data = array(
        'name' => null,
        'date' => null,
        'cube' => array(
            'EUR' => 1
        )
    );

    /**
     * Get the current currencies of the European Central Bank XML file.
     *
     * @return Array Return bank name, date version and currencies values
     */
    public function __construct( ) {

        Hoa_Stream_Context::getInstance('european_central_bank', 'http')->addOptions(array(
            'timeout' => 30
        ));

        try {

            $content = new Hoa_File_Read(
                self::URL,
                Hoa_File::MODE_READ,
                'european_central_bank'
            );
        }
        catch ( Hoa_File_Exception $e ) {

            throw new Hoa_Convertor_Exception(
                $e->getFormattedMessage(),
                $e->getCode()
            );
        }

        $xml = Hoa_Xml::parse($content->readAll(), null);

        if (!$xml) {
            throw new Hoathis_Currency_Exception('XML document %s is not valid', 1, array(self::URL));
        }

        $this->data['name'] = $xml['gesmes:Sender']['gesmes:name'];
        $this->data['date'] = $xml['Cube']['Cube-ATTR']['time'];
        $this->data['cube']['EUR'] = 1;

        foreach($xml['Cube']['Cube']['Cube-ATTR'] AS $k => $v)
            $this->data['cube'][$v['currency']] = $v['rate'];

        return $this->data;
    }

    /**
     * Get specific currency
     *
     * @return float Currency value
     */
    public function __get ( $property ) {

        if (!isset($this->data['cube'][$property]))
            throw new Hoathis_Currency_Exception('Currency %s does not exist.', 2, array($property));

        return $this->data['cube'][$property];
    }

    /**
     * Get bank name
     *
     * @return String Bank name
     */
    public function getName () {

        return !is_null($this->data['name'])
                ? $this->data['name']
                : NULL;
    }

    /**
     * Get date version
     *
     * @return String Date version
     */
    public function getDate () {

        return !is_null($this->data['date'])
                ? $this->data['date']
                : NULL;
    }

    /**
     * Get currencies values
     *
     * @return Array Currencies values
     */
    public function getCurrencies () {

        return count($this->data['cube']) > 1
                ? $this->data['cube']
                : NULL;
    }
}